<template>
  <div class="col main-content-edit " style="margin-top: 15px;">
    <div class="row">
      <div class="col">
        <el-tabs type="border-card">
          <el-tab-pane label="基础信息">
            <el-form :model="customer" labelPosition="top" :rules="rules" ref="form" class="custom-form">
              <el-form-item label="用户名" prop="userName">
                <div class="row">
                  <div class="col col-3">
                    <el-input v-model="customer.userName" clearable placeholder="请输入用户名,可以是QQ号"></el-input>
                  </div>
                  <div class="col col-9"><span>登录系统的账号可以使用QQ号等</span>
                  </div>
                </div>
              </el-form-item>

              <el-form-item label="昵称" prop="nickName">
                <div class="row">
                  <div class="col col-3">
                    <el-input v-model="customer.nickName" clearable placeholder="请输入昵称"></el-input>
                  </div>
                  <div class="col col-9"><span>用来显示的名称</span>
                  </div>
                </div>
              </el-form-item>
              <el-form-item label="账户密码" prop="password">
                <div class="row">
                  <div class="col col-3">
                    <el-input type="password" clearable v-model="customer.password" placeholder="请输入用户密码"></el-input>
                  </div>
                  <div class="col col-9">
                    <span>请输入不少于8位的数字、字符等组合</span>
                  </div>
                </div>
              </el-form-item>

              <el-form-item label="重复密码" prop="repassword">
                <div class="row">
                  <div class="col col-3">
                    <el-input type="password" clearable v-model="customer.repassword"
                              placeholder="请再次输入用户密码"></el-input>
                  </div>
                  <div class="col col-9">
                    <span>输入重复密码和密码保持一致</span>
                  </div>
                </div>
              </el-form-item>

              <el-form-item label="邮箱地址" prop="email">
                <div class="row">
                  <div class="col col-3">
                    <el-input v-model="customer.email" clearable placeholder="请输入邮箱地址"></el-input>
                  </div>
                  <div class="col col-9"><span>输入邮箱地址,将用来接收信息</span>
                  </div>
                </div>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="用户地址">
            <el-form :model="customer.address" labelPosition="top" :rules="rules" ref="address-form"
                     class="custom-form">

              <el-form-item label="收件人:" prop="name">
                <div class="row">
                  <div class="col col-3">
                    <el-input v-model="customer.address.name" clearable placeholder="请输入收件人称呼"></el-input>
                  </div>
                  <div class="col col-9"><span>请输入收件人名称</span>
                  </div>
                </div>
              </el-form-item>

              <el-form-item label="所在城市:" prop="district">
                <div class="row">
                  <div class="col col-3">
                    <h-district-choose @change="addressChange"
                                       v-model="customer.address.districtIds"></h-district-choose>
                  </div>
                  <div class="col col-9"><span>请选择用户所在的城市</span>
                  </div>
                </div>
              </el-form-item>
              <el-form-item label="详细地址:" prop="address">
                <div class="row">
                  <div class="col col-3">
                    <el-input v-model="customer.address.address" clearable placeholder="请输入信息地址"/>
                  </div>
                  <div class="col col-9"><span>请输入详细地址精确到门牌</span>
                  </div>
                </div>
              </el-form-item>
              <el-form-item label="联系电话:" prop="mobile">
                <div class="row">
                  <div class="col col-3">
                    <el-input v-model="customer.address.mobile" clearable placeholder="请输入联系电话"/>
                  </div>
                  <div class="col col-9"><span>请输入联系电话</span>
                  </div>
                </div>
              </el-form-item>
              <el-form-item label="邮政编码:">
                <div class="row">
                  <div class="col col-3">
                    <el-input v-model="customer.address.zipcode" clearable placeholder="请输入邮政编码"/>
                  </div>
                  <div class="col col-9"><span>请输入邮政编码</span>
                  </div>
                </div>
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <div style="margin-top: 30px;">
            <el-button type="primary" @click="submitForm"><i class="fa fa-plus"></i> 添加</el-button>
            <el-button @click="goback" type="success"><i class="fa fa-sign-out"></i> 返回</el-button>
          </div>
        </el-tabs>
      </div>
    </div>
  </div>
</template>
<script>
  import AdminService from 'src/service/admin'
  import HDistrictChoose from 'src/components/common/HDistrictChoose'

  export default {
    name: 'HCreateCustomer',
    components: {
      HDistrictChoose
    },
    data() {
      const checkPassword = (rule, value, callback) => {
        if (value == null) {
          callback(new Error('请再次输入密码!'))
        } else {
          if (this.customer.password !== value) {
            callback(new Error('两次输入的密码不一致!'))
          } else {
            callback()
          }
        }
      }
      return {
        customer: {
          id: null,
          status: 1,
          userName: '',
          nickName: '',
          type: 2,
          birthday: '',
          sex: '',
          email: '',
          qq: '',
          weixin: '',
          figureUrl: '',
          repassword: '',
          address: {
            name: '',
            zipcode: '',
            address: '',
            mobile: '',
            districtIds: null,
            district: ''
          }
        },
        rules: {
          userName: [
            {required: true, message: '请输入用户名称', trigger: 'blur'},
            {min: 4, message: '最少4个字符', trigger: 'blur'}
          ],
          district: [
            {required: true, message: '请选择所在的城市', trigger: 'blur'}
          ],
          address: [
            {required: true, message: '请输入详细地址', trigger: 'blur'},
            {min: 1, message: '请输入详细地址', trigger: 'blur'}
          ],
          mobile: [
            {required: true, message: '请输入联系电话', trigger: 'blur'},
          ],
          name: [
            {required: true, message: '请输入收件人的称呼', trigger: 'blur'}
          ],
          nickName: [
            {required: true, message: '请输入昵称', trigger: 'blur'},
            {min: 1, message: '最少1个字符', trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入用户密码', trigger: 'blur'}

          ],
          repassword: [
            {required: true, message: '请再次输入密码', trigger: 'blur'},
            {validator: checkPassword, trigger: 'blur'}
          ],
          email: [
            {required: true, message: '请输入邮箱地址', trigger: 'blur'},
            {type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur,change'}]
        }
      }
    },
    methods: {
      addressChange: function (item) {
        this.customer.address.district = item.names.join('/')
      },
      submitForm: function (go) {
        const me = this
        this.$refs['form'].validate((valid) => {
          if (valid) {
            AdminService.customerCreate(me.customer).then(function (data) {
              if (data != null) {
                me.$message.success('创建用户成功!')
                if (go) {
                  me.$router.push('/admin/customer/customers.html')
                } else {
                  me.restForm()
                }
              } else {
                me.$message.error(data.message)
              }
            })
          } else {
            return false
          }
        })
      },
      goback: function () {
        this.$router.go(-1)
      },
      restForm: function () {
        this.customer = {
          id: null,
          status: 1,
          userName: '',
          nickName: '',
          type: 2,
          birthday: '',
          sex: '',
          email: '',
          qq: '',
          weixin: '',
          figureUrl: '',
          repassword: '',
          address: {
            name: '',
            zipcode: '',
            address: '',
            mobile: '',
            districtIds: null,
            district: ''
          }
        }
        this.$refs['form'].resetFields()
      }
    },
    mounted: function () {

    }
  }
</script>
